package com.jindi.appuser.service.impl;

import com.jindi.appuser.common.EmptyUtil;
import com.jindi.appuser.common.ServerResponse;
import com.jindi.appuser.dao.YysPriceMapper;
import com.jindi.appuser.domain.YysPrice;
import com.jindi.appuser.service.PriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PriceServiceImpl implements PriceService {
    @Autowired
    private YysPriceMapper yysPriceMapper;

    @Override
    public ServerResponse priceList() {
        List<YysPrice> list = yysPriceMapper.listPrice();
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse priceInfo(String id) {
        if (EmptyUtil.isEmpty(id)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        YysPrice y = yysPriceMapper.selectByPrimaryKey(Integer.parseInt(id));
        if (EmptyUtil.isEmpty(y)) {
            return ServerResponse.createByErrorMessage("价格信息不存在");
        }
        return ServerResponse.createBySuccess(y);
    }

    @Override
    public ServerResponse priceUpdate(YysPrice yysPrice) {
        String price = yysPrice.getPrice();
        Integer count = yysPrice.getCount();
        Integer id = yysPrice.getId();
        if (id == null || id == 0 || count == null || count == 0 || EmptyUtil.isEmpty(price)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Integer index = yysPriceMapper.updateByPrimaryKeySelective(yysPrice);
        if (index < 1) {
            return ServerResponse.createByErrorMessage("修改失败");
        }
        return ServerResponse.createBySuccess("修改成功");
    }
}
